<?xml version="1.0" encoding="UTF-8"?>
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="ja" lang="ja"
      xmlns:xul="http://www.mozilla.org/keymaster/gatekeeper/there.is.only.xul">
<head>
  <title>Ubiquity Web Search Commands for Japanese, nora - goo-dictionary</title>
</head>
<body>
<p>This xhtml source contains commands for searching the Web.  Its
corresponding feed is <a href="ubiquity-commands-ja-nora-goo-dictionary.html">here</a>.</p>


<div id="goodict" style="display: none;">
<b>${query}</b>でgoo辞書を検索:
<br/><br/>
{for item in items}
  <div style="clear: both;font-size:90%;">
    <b>${item.key} <a href="${item.url}" accesskey="${item.key}">${item.entry}</a></b>
    <br/>
    <div style="margin-left: 20px; font-size: 90%;">
      ${item.meanings}
      <br/>
      <A href="${item.dictionaryUrl}">${item.dictionary}</A>
    </div>
    <div style="font-size:60%;clear:both;"> <br/></div>
  </div>
{forelse}
  <b>該当項目が見つかりませんでした</b>
{/for}
    <div style="font-size:30%;clear:both;"> <br/></div>
    <div style="font-size:70%;clear:both;">※[Ctrl]+[Alt]+[番号] でジャンプ</div>
</div>


<script class="commands"><![CDATA[


CmdUtils.CreateCommand({
    name: "goo-dictionary",
    description: "goo辞書",
    takes: {search: noun_arb_text},
    icon: "http://dictionary.goo.ne.jp/favicon.ico",
	homepage: "http://code.google.com/p/ubiquity-commands-ja-nora/",
	author: {name: "hokunarin", email: "hokunarin@gmail.com"},
	license: "MPL",

    preview: function( previewBlock, directObject ) {

        var searchText = jQuery.trim(directObject.text);
        if(searchText.length < 1) {
            var previewStr = "<small>goo辞書は無料で使える日本最大級の辞書サービスです。<br/>和英辞典/英和辞典/国語辞典/四字熟語/MBA用語辞典/IT用語辞典/文例集などを提供しています。</small>";
            previewBlock.innerHTML = previewStr;
            return;
        }

        var previewTemplate = "<b>${query}</b> を goo 辞書で検索します...";
        var previewData = {query: searchText};
        previewBlock.innerHTML = CmdUtils.renderTemplate(previewTemplate, previewData);
        
        //Utils.reportInfo(getGooDicUrl(searchText));
        
        CmdUtils.previewAjax(previewBlock, {
          type: "GET",
          url: getGooDicUrl(searchText),
          dataType: "html",
          
          error: function() {
            previewBlock.innerHTML = "Error searching Goo Dictionary.";
          },
          
          success: function(responseData) {
            
            responseData = jQuery(responseData);
            var items = [];
            var i = 0;
            
            responseData.find("h2.ch04 + .allResultList").each(function(){
                
                var allResultList = jQuery(this);
                
                allResultList.find("A").attr("href", function(){
                    return addBaseUrl(this, "http://ext.dictionary.goo.ne.jp");
                });
                
                var entry = allResultList.find(".entry:first").text();
                var url = allResultList.find(".entry:first a").attr("href");
                var meanings = allResultList.find(".meanings:first").text();
                var dictionary = allResultList.find("DIV:eq(1)").text();
                var dictionaryUrl = allResultList.find("DIV:eq(1) a").attr("href");
                var key = ++i;
                
                var item = {key : key,
                            url : url,
                            meanings : meanings,
                            entry: entry,
                            dictionary : dictionary,
                            dictionaryUrl : dictionaryUrl};
                
                items.push(item);
            });
            
            
            var previewData = {
              query : searchText,
              items : items
            };
            
            previewBlock.innerHTML = CmdUtils.renderTemplate(
              jQuery("#goodict", feed.dom).html(),
              previewData);
          }
        });

    },

    execute: function( directObject ) {
        Utils.openUrlInBrowser( getGooDicUrl(directObject.text) );
    }
});

function getGooDicUrl(query){
    var url = "http://dictionary.goo.ne.jp/freewordsearcher.html?MT={searchTerms}&IE=UTF-8&OE=UTF-8&kind=all";
    url = url.replace(/%s|{searchTerms}/g, encodeURI(query) );
    return url;
}


function addBaseUrl(href, baseUrl){
    
    uri = parseUri(href);
    
    if(uri.host == ""){
        href = baseUrl + href;
    }else if(uri.host == "ubiquity"){
        href = baseUrl + uri.relative;
    }
    
    return href;
}


/*
	parseUri 1.2.1
	(c) 2007 Steven Levithan <stevenlevithan.com>
	MIT License
*/

function parseUri (str) {
	var	o   = parseUri.options,
		m   = o.parser[o.strictMode ? "strict" : "loose"].exec(str),
		uri = {},
		i   = 14;

	while (i--) uri[o.key[i]] = m[i] || "";

	uri[o.q.name] = {};
	uri[o.key[12]].replace(o.q.parser, function ($0, $1, $2) {
		if ($1) uri[o.q.name][$1] = $2;
	});

	return uri;
};

parseUri.options = {
	strictMode: true,
	key: ["source","protocol","authority","userInfo","user","password","host","port","relative","path","directory","file","query","anchor"],
	q:   {
		name:   "queryKey",
		parser: /(?:^|&)([^&=]*)=?([^&]*)/g
	},
	parser: {
		strict: /^(?:([^:\/?#]+):)?(?:\/\/((?:(([^:@]*):?([^:@]*))?@)?([^:\/?#]*)(?::(\d*))?))?((((?:[^?#\/]*\/)*)([^?#]*))(?:\?([^#]*))?(?:#(.*))?)/,
		loose:  /^(?:(?![^:@]+:[^:@\/]*@)([^:\/?#.]+):)?(?:\/\/)?((?:(([^:@]*):?([^:@]*))?@)?([^:\/?#]*)(?::(\d*))?)(((\/(?:[^?#](?![^?#\/]*\.[^?#\/.]+(?:[?#]|$)))*\/?)?([^?#\/]*))(?:\?([^#]*))?(?:#(.*))?)/
	}
};


]]></script>
</body>
</html>
